﻿using System;
using System.ComponentModel;
using System.Windows;
using OpenFileTransfer.Wpf.Models;
using OpenFileTransfer.Wpf.ViewModels;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;

namespace OpenFileTransfer.Wpf.Views
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    // ReSharper disable once RedundantExtendsListEntry
    public partial class MainWindow : Window
    {
        private readonly TitleView _titleView;

        private MainView? _mainView;
        //private UpgradeView? _upgradeView;
        //private AboutView? _aboutView;
        //private HistoryView? _historyView;
        //private SettingView? _settingView;
        //private FeedbackView? _feedbackView;
        //private FrameworkView? _frameworkView;

        private readonly IContainerExtension _container;
        private readonly IRegionManager _regionManager;
        private IRegion? _region;

        public MainWindow(
            IContainerExtension container,
            IRegionManager regionManager,
            IEventAggregator eventAggregator
        )
        {
            InitializeComponent();

            _container = container;
            _regionManager = regionManager;

            _titleView = new TitleView();
            _titleView.OnMinimize += () => WindowState = WindowState.Minimized;
            _titleView.OnMaxRestore += OnMaxRestore;
            _titleView.OnClose += Close;
            _titleView.OnMenuItemClicked += OnMenuItemClicked;

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                DataContext = new MainWindowViewModel(eventAggregator);
            }
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            //注册标题
            var titleRegion = _regionManager.Regions["TitleRegion"];
            titleRegion.Add(_titleView);
            titleRegion.Activate(_titleView);

            //注册View
            {
                //解析View
                _mainView = _container.Resolve<MainView>();
                //_upgradeView = _container.Resolve<UpgradeView>();
                //_aboutView = _container.Resolve<AboutView>();
                //_historyView = _container.Resolve<HistoryView>();
                //_settingView = _container.Resolve<SettingView>();
                //_feedbackView = _container.Resolve<FeedbackView>();
                //_frameworkView = _container.Resolve<FrameworkView>();

                //添加View
                _region = _regionManager.Regions["ContentRegion"];
                _region.Add(_mainView);
                //_region.Add(_upgradeView);
                //_region.Add(_aboutView);
                //_region.Add(_historyView);
                //_region.Add(_settingView);
                //_region.Add(_feedbackView);
                //_region.Add(_frameworkView);
            }
        }

        private void MainWindow_OnStateChanged(object? sender, EventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                _titleView.MaxWindow = true;
            }
        }

        private void OnMaxRestore()
        {
            if (WindowState != WindowState.Normal && WindowState != WindowState.Maximized)
            {
                return;
            }

            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
                _titleView.MaxWindow = false;
            }
            else
            {
                var maxWindow = (int)Width != 1280;
                _titleView.MaxWindow = !maxWindow;

                Width = maxWindow ? 1280 : SystemParameters.PrimaryScreenWidth;
                Height = maxWindow ? 720 : SystemParameters.PrimaryScreenHeight;
                Left = maxWindow ? (SystemParameters.PrimaryScreenWidth - 1280) / 2 : 0;
                Top = maxWindow ? (SystemParameters.PrimaryScreenHeight - 720) / 2 : 0;
            }
        }

        private void OnMenuItemClicked(MenuType menuType)
        {
            switch (menuType)
            {
                case MenuType.Main:
                    _region?.Activate(_mainView);
                    break;
                //    case MenuType.Upgrade:
                //        _region?.Activate(_upgradeView);
                //        break;
                //    case MenuType.About:
                //        _region?.Activate(_aboutView);
                //        break;
                //    case MenuType.History:
                //        _region?.Activate(_historyView);
                //        break;
                //    case MenuType.Setting:
                //        _region?.Activate(_settingView);
                //        break;
                //    case MenuType.Feedback:
                //        _region?.Activate(_feedbackView);
                //        break;
                //    case MenuType.Framework:
                //        _region?.Activate(_frameworkView);
                //        break;
            }
        }
    }
}
